package com.mfnote.db.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.mfnote.db.DBHelper;
import com.mfnote.db.DBInfo;
import com.mfnote.model.MainCountEntity;
import com.mfnote.model.WriteEntity;

/**
 * Write模块数据库操作类
 * 
 * @author ZHF
 * 
 */
public class WriteDao {

	public SQLiteDatabase db; // 系统提供的数据库
	private DBHelper dbHelper = null;
	
	public WriteDao(Context context) {
		dbHelper = DBHelper.getInstance(context);//用static方法来实例化该对象
	}

	/** 打开数据库 **/
	public void openDatabase() {
			db = dbHelper.getWritableDatabase(); // 这里根据数据库名和版本号判断，第一次就调用onCreate创建表，第二次若两者相同就直接返回，若不同调用onUpgrade
	}
	
	/** 插入记事本数据 **/
	public long insertWrite(WriteEntity writeEntity) {
		// 打开数据库
		openDatabase();

		ContentValues values = new ContentValues();

		values.put("title", writeEntity.getTitle());
		values.put("content", writeEntity.getContent());
		values.put("imagePath", writeEntity.getImagePath());
		values.put("videoPath", writeEntity.getVideoPath());
		values.put("time", writeEntity.getTimeNow());
		values.put("color", writeEntity.getColor());
		values.put("background", writeEntity.getBackground());
		values.put("lock", writeEntity.getLock());

		long raw = db.insert(DBInfo.Table.TB_WRITE_NAME, null, values);
		// db.execSQL("insert into write(title, content) values ('测试数据','哪里出错了');");
		db.close();
		return raw; //返回的是当前受影响的行号
	}

	/** 查询记事本表的所有记录 **/
	public ArrayList<HashMap<String, Object>> getWriteAllCount() {
		// 打开数据库
		openDatabase();

		ArrayList<HashMap<String, Object>> data = null; // 接收数据

		// Cursor----ResultSet:用于接收数据库数据列表
		Cursor cursor = db.query(DBInfo.Table.TB_WRITE_NAME, null, null, null,null, null, null);

		if (null != cursor && cursor.getCount() > 0) {
			data = new ArrayList<HashMap<String, Object>>();

			while (cursor.moveToNext()) {
				HashMap<String, Object> map = new HashMap<String, Object>();

				map.put("title",cursor.getString(cursor.getColumnIndex("title")));
				map.put("content",cursor.getString(cursor.getColumnIndex("content")));
				map.put("imagePath",cursor.getString(cursor.getColumnIndex("imagePath")));
				map.put("videoPath",cursor.getString(cursor.getColumnIndex("videoPath")));
				map.put("time", cursor.getString(cursor.getColumnIndex("time")));
				map.put("color",cursor.getString(cursor.getColumnIndex("color")));
				map.put("background",cursor.getInt(cursor.getColumnIndex("background")));
				map.put("lock",cursor.getInt(cursor.getColumnIndex("lock")));

				data.add(map);
			}
		}
		cursor.close();
		db.close(); // 用完记得要关闭哦
		return data;
	}

	/** 更新记事本表的记录--根据标题名称 **/
	public long updateWrite(WriteEntity writeEntity) {

		// 打开数据库
		openDatabase();

		ContentValues values = new ContentValues();

		values.put("title", writeEntity.getTitle());
		values.put("content", writeEntity.getContent());
		values.put("imagePath", writeEntity.getImagePath());
		values.put("videoPath", writeEntity.getVideoPath());
		values.put("time", writeEntity.getTimeNow());
		values.put("color", writeEntity.getColor());
		values.put("background", writeEntity.getBackground());
		values.put("lock", writeEntity.getLock());

		long raws = db.update(DBInfo.Table.TB_WRITE_NAME, values, "title=?",new String[] { writeEntity.getTitle() });
		db.close(); // 用完记得要关闭哦
		return raws;  //返回的是当前受影响的行号
	}

	/** 更新记事本表的加锁记录--根据标题名称 **/
	public long updateWrite(String title,int lock) {
		
		// 打开数据库
		openDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("lock", lock);
		
		long raws = db.update(DBInfo.Table.TB_WRITE_NAME, values, "title=?",new String[] {title});
		db.close(); // 用完记得要关闭哦
		return raws;  //返回的是当前受影响的行号
	}

	/** 删除记事本表的记录 **/
	public long deleteWrite(HashMap<String, Object> map) {
		// 打开数据库
		openDatabase();

		long raws = db.delete(DBInfo.Table.TB_WRITE_NAME, "title=?", new String[] { map.get("title").toString() });

		db.close();
		return raws; //返回的是当前受影响的行号number of rows affected 
	}

	/**根据进入的是哪一项获取对应图片保存路径信息**/
	public String getImagesPath(String title) {
		// 打开数据库
		openDatabase();
		
		String imagesPath = ""; //接收数据库里返回的多张图片的路径集

		// Cursor----ResultSet:用于接收数据库数据列表
		Cursor cursor = db.query(DBInfo.Table.TB_WRITE_NAME, new String[]{"imagePath"}, "title=?",new String[]{title},null, null, null);
		
		if (null != cursor && cursor.getCount() > 0) {

			while (cursor.moveToNext()) {
				
			   imagesPath = cursor.getString(cursor.getColumnIndex("imagePath"));
				
				System.out.println(imagesPath.toString());
			}
		}
		cursor.close();
		db.close();
		return imagesPath;
	}
	/**根据进入的是哪一项获取对应录音保存路径信息**/
	public String getVideoPath(String title) {
		// 打开数据库
		openDatabase();
		
		String videoPath = ""; //接收数据库里返回的多张图片的路径集
		
		// Cursor----ResultSet:用于接收数据库数据列表
		Cursor cursor = db.query(DBInfo.Table.TB_WRITE_NAME, new String[]{"videoPath"}, "title=?",new String[]{title},null, null, null);
		
		if (null != cursor && cursor.getCount() > 0) {
			
			while (cursor.moveToNext()) {
				
				videoPath = cursor.getString(cursor.getColumnIndex("videoPath"));
				
				System.out.println(videoPath.toString());
			}
		}
		cursor.close();
		db.close();
		return videoPath;
	}
    
	/**根据名称进行查询显示数据
	 *  select * from write where title = name ;**/
	public ArrayList<HashMap<String, Object>> queryByTitle(String name) {
		// 打开数据库
		openDatabase();

		ArrayList<HashMap<String, Object>> data = null; // 接收数据

		// Cursor----ResultSet:用于接收数据库数据列表
		Cursor cursor = db.query(DBInfo.Table.TB_WRITE_NAME, null, "title=?", new String[]{name},null, null, null);

		if (null != cursor && cursor.getCount() > 0) {
			data = new ArrayList<HashMap<String, Object>>();

			while (cursor.moveToNext()) {
				HashMap<String, Object> map = new HashMap<String, Object>();

				map.put("title",cursor.getString(cursor.getColumnIndex("title")));
				map.put("content",cursor.getString(cursor.getColumnIndex("content")));
				map.put("imagePath",cursor.getString(cursor.getColumnIndex("imagePath")));
				map.put("videoPath",cursor.getString(cursor.getColumnIndex("videoPath")));
				map.put("time", cursor.getString(cursor.getColumnIndex("time")));
				map.put("color",cursor.getString(cursor.getColumnIndex("color")));
				map.put("background",cursor.getInt(cursor.getColumnIndex("background")));
				map.put("lock",cursor.getInt(cursor.getColumnIndex("lock")));

				data.add(map);
			}
		}
		cursor.close();
		db.close(); // 用完记得要关闭哦
		return data;
	}

	/**查询记录条数**/
	public long queryCount() {
		// 打开数据库
		openDatabase();
		
		String sql = "select count() from "+DBInfo.Table.TB_WRITE_NAME;
		SQLiteStatement statement = db.compileStatement(sql);
		long count = statement.simpleQueryForLong();
		return count;
	}
}
